﻿using System;
using System.Data;
using Pantry.Business;
using Pantry.Commons;

namespace Pantry.Web.Control
{
    public partial class SupportingMaterial : System.Web.UI.UserControl
    {
        private OrganisationBus _orBus;
        private DataView _dataView;
        private Entity.SupportingMaterial _sm;

        protected void Page_Load(object sender, EventArgs e)
        {
            CollectionPager.DataSourcePaged.CurrentPageIndex = 1;
            FillSupportingMaterialToGridview(CheckboxIsInActive.Checked);
            //_orBus.SelectSupportingMaterialById(
            //    TextBoxEditUrl.Text, 
            //    TextBoxEditDes.Text,
            //    DropDownListEditType.SelectedItem.Text, 
            //    TextBoxAddedBy.Text, 
            //    TextBoxAddedDate.Text,
            //    Convert.ToInt16(TextBoxId.Text));
        }

        /// <summary>
        /// Fills the Supporting Material to gridview.
        /// </summary>
        public void FillSupportingMaterialToGridview(bool isInActived)
        {
            try
            {
                _orBus = new OrganisationBus();
                DataTable dataTable = _orBus.SelectAllSupportingMaterial();
                _dataView = dataTable.DefaultView;
                if (!isInActived)
                {
                    _dataView.RowFilter = "IsActive = 'true'";
                    _dataView.Sort = "Type ASC";
                }

                CollectionPager.PageSize = 15;
                CollectionPager.BindToControl = RepeaterListSupportingMaterial;
                CollectionPager.DataSource = _dataView;
                RepeaterListSupportingMaterial.DataSource = CollectionPager.DataSourcePaged;
                RepeaterListSupportingMaterial.DataBind();
            }
            catch (ApplicationException ex)
            {
                ExceptionHandler.WriteErrorLog(ex);
            }
        }

        protected void SaveEditClick(object sender, EventArgs e)
        {
            _orBus = new OrganisationBus();
            _sm = new Entity.SupportingMaterial
                      {
                          Url = TextBoxEditDes.Text.Trim(),
                          Description = TextBoxEditDes.Text.Trim(),
                          Type = DropDownListEditType.SelectedItem.Text,
                          Id = Convert.ToInt16(TextBoxId.Text)
                      };
            _orBus.UpdateSupportingMaterial(_sm);
            PanelPopupDetailtSupporting.Visible = false;
        }

        protected void CloseEditClick(object sender, EventArgs e)
        {
            PanelPopupDetailtSupporting.Visible = false;
        }

        protected void UrlClick(object sender, EventArgs e)
        {
            PanelPopupDetailtSupporting.Visible = true;
        }
    }
}